package com.mofang.powerdekorhelper.base.recycleview;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

/**
 * Created by luofei on 2016/9/19 0019.
 */
public abstract class BaseRecycleViewAdapter extends RecyclerView.Adapter<BaseViewHolder> {

    private int layoutId;
    private OnItemClickListner onItemClickListner;//单击事件
    private OnItemLongClickListner onItemLongClickListner;//长按单击事件
    private boolean clickFlag = true;//单击事件和长单击事件的屏蔽标识
    public LayoutInflater mInflater = null;
    public Context mContext = null;
    public List mData = null;

    public BaseRecycleViewAdapter(List<?> data, Context mContext, int layoutId) {
        this.mData = data;
        this.mContext = mContext;
        this.layoutId = layoutId;
        mInflater = LayoutInflater.from(mContext);
    }

    @Override
    public BaseViewHolder onCreateViewHolder(final ViewGroup parent, final int viewType) {

        View v = LayoutInflater.from(mContext).inflate(layoutId, parent, false);
        final BaseViewHolder holder = new BaseViewHolder(v, mContext);
        //单击事件回调
        v.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (clickFlag) {
                    if (onItemClickListner != null) {
                        onItemClickListner.onItemClickListner(parent, v, holder.getLayoutPosition());
                    }
                }
                clickFlag = true;
            }
        });
        //单击长按事件回调
        v.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (onItemLongClickListner != null) {
                    onItemLongClickListner.onItemLongClickListner(parent, v, holder.getLayoutPosition());
                }
                clickFlag = false;
                return false;
            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(BaseViewHolder holder, int position) {

        convert(holder, mData.get(position), position);
    }

    protected abstract <T> void convert(BaseViewHolder holder, T bean, int position);


    @Override
    public int getItemCount() {
        return mData.size();
    }

    public void setOnItemClickListner(OnItemClickListner onItemClickListner) {
        this.onItemClickListner = onItemClickListner;
    }

    public void setOnItemLongClickListner(OnItemLongClickListner onItemLongClickListner) {
        this.onItemLongClickListner = onItemLongClickListner;
    }

    public interface OnItemClickListner {
        void onItemClickListner(ViewGroup parent, View v, int position);
    }

    public interface OnItemLongClickListner {
        void onItemLongClickListner(ViewGroup parent, View v, int position);
    }


}
